package project.daggerdemo.network.api;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import project.daggerdemo.network.api.response.UserResponse;
import project.daggerdemo.session.bean.User;

/**
 * Created by lgs on 2018/3/3.
 */

public class UserManager {

    private GitHubApiService gitHubApiService;

    public UserManager(GitHubApiService gitHubApiService) {
        this.gitHubApiService = gitHubApiService;
    }

    public Observable<User> getUser(String username) {
        Observable<User> userObservable = gitHubApiService.getUser(username)
                .map(new Function<UserResponse, User>() {
                    @Override
                    public User apply(@NonNull UserResponse userResponse) throws Exception {
                        User user = new User();
                        user.login = userResponse.login;
                        user.id = userResponse.id;
                        user.url = userResponse.url;
                        user.email = userResponse.email;
                        return user;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

        return userObservable;
    }
}
